package com.spring5.testdemo;

import com.spring5.autowire.Emp;
import com.spring5.bean.Orders;
import com.spring5.collectiontype.Book;
import com.spring5.collectiontype.Course;
import com.spring5.collectiontype.Stu;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5Demo1 {

  @DisplayName("集合类型属性注入")
  @Test
  public void testCollection1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    Stu stu = context.getBean("stu", Stu.class);
    stu.test();
  }

  @DisplayName("bean默认单实例，修改为多实例")
  @Test
  public void testCollection2() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
    Book book1 = context.getBean("book", Book.class);
    Book book2 = context.getBean("book", Book.class);
    // book.test();
    System.out.println(book1);
    System.out.println(book2);
  }

  @DisplayName("FactoryBean创建：定义类型可以和返回类型不相同")
  @Test
  public void test3() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    Course course = context.getBean("myBean", Course.class);
    System.out.println(course);
  }

  @DisplayName("Bean的生命周期")
  @Test
  public void testBean3() {
    //        ApplicationContext context =
    //                new ClassPathXmlApplicationContext("bean4.xml");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    Orders orders = context.getBean("orders", Orders.class);
    System.out.println("第四步 获取创建bean实例对象");
    System.out.println(orders);

    // 手动让bean实例销毁
    context.close();
  }

  @DisplayName("bean的自动注入")
  @Test
  public void test4() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
    Emp emp = context.getBean("emp", Emp.class);
    System.out.println(emp);
  }
}
